home *** CD-ROM | disk | FTP | other *** search
- Path: lantana.singnet.com.sg!usenet
- From: s8700055@singnet.com.sg (XY Xie)
- Newsgroups: comp.lang.c
- Subject: Novice programmer needs help!
- Date: Sat, 16 Mar 1996 17:10:53 GMT
- Organization: Singapore Telecom Internet Service
- Message-ID: <4ieov7$r5@lantana.singnet.com.sg>
- NNTP-Posting-Host: ts900-2822.singnet.com.sg
- X-Newsreader: Forte Free Agent 1.0.82
-
- I'm learning C programming myself from a book and I recently wrote
- this programme and I could not figure out why the errors occured.
- Could some kind soul out there check the code for me? I used upper
- case for all the error messages and I included them on a new line
- following the supposedly erroneous code (just to separate them from
- the usual programming comments).
-
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define totnum 20 //total no. of entries in list=20
-
- struct list{
- char name[15]; //alloc 15 chars for name
- char sex;
- int age;
- };
-
- void displaymenu(void); //display menu for choice
- char getchoice(void); //get a choice from user
- void addlist(struct list *friend[totnum]); //add items to list
- void allocatemem(struct list *friend[totnum]); //allocate memory
- void displist(struct list *friend[totnum]); //display list
- void search(struct list *friend[totnum]); //search a name
- void dealloc(struct list *friend[totnum]); //deallocate mem
- // THE ABOVE 5 LINES GOT "ERROR: ) EXPECTED"
-
- int num=0; //current no. of entries
-
- main()
- {
- char choice;
- struct list *friend[numtot];
- //"ERROR:DECLARATION TERMINATED INCORRECTLY IN FUNCTION MAIN()"
-
- do
- {displaymenu();
- choice=getchoice();
-
- switch(choice)
- {case('a'):{addlist(friend);
- break;}
- case('b'):{displist(friend);
- break;}
- case('c'):{search(friend);
- break;}
- case('d'):{dealloc(friend);
- exit(1);
- break;}
- //ALL THE 4 FUNCTIONS I CALLED ABOVE HAVE THESE:
- //"ERROR : CALL TO UNDEFINED FUNCTION IN FUNCTION MAIN()" and
- //"ERROR : EXPRESSION SYNTAX IN FUNCTION MAIN()"
-
- default:{puts("Wrong entry. Try again");
- break;}
- }
- } while(choice!='d');
- return 0;
- }
- //*****************************************************
- void displaymenu(void) //display menu
- {
- puts("\n\t\tFRIENDS' PARTICULARS LIST");
- puts("\t\t-------------------------\n");
- puts("a. Add to list.");
- puts("b. Display list.");
- puts("c. Get person.");
- puts("d. Exit program.");
- printf("Enter your choice.(a,b,c or d):");
- return;
- }
- //*****************************************************
- char getchoice(void) //input choice
- {
- char ans;
- fflush(stdin);
- ans=getche();
- ans=tolower(ans); //convert all input to lower case
- printf("\n");
- return ans;
- }
- //*****************************************************
- void addlist(struct list *friend[]) //add to list
- // "ERROR: ) EXPECTED"
-
- {
- if(num==totnum)
- {puts("List is full");
- return;}
- allocatemem(friend); //alloc mem for 1 structure variable
- printf("\nEnter name. ");
- fflush(stdin);
- fgets(friend[num]->name[],15,stdin);
- printf("Enter sex. ");
- fflush(stdin);
- friend[num]->sex=getchar();
- printf("Enter age. ");
- scanf(" %d", &friend[num]->age);
- num++;
- return;
- }
- //***************************************************
- void allocatemem(struct list *friend[]) //alloc heap mem
- {
- friend[num]=(struct list*)malloc(sizeof(struct list));
- if(!friend[num])
- {puts("Memory error,");
- exit(1);}
- return;
- }
- //*****************************************************
- void displist(struct list *friend[]) //display entries and
- { //calc average age
- int c;
- float avg=friend[0]->age;
- if(num==0)
- {puts("\n\nThere is no name on the list");
- return;}
- for(c=0;c<num;c++)
- {
- printf("\n #%d\n",c+1);
- printf(" name:%ssex:%c\nage:%d\n",friend[c]->name[],
- friend[c]->sex,friend[c]->age);
- if(c!=0)
- {avg+=friend[c]->age;}
- }
- avg/=(float)c;
- printf("\n Average age is %.1f.\n",avg);
- return;
- }
- //******************************************************************
- void search(struct list *friend[])
- {
- int c,match=0;
- char ss[15];
- printf("\nEnter name to search. ");
- fflush(stdin);
- fgets(ss,15,stdin);
- for(c=0;c<num;c++)
- {ss[c]=tolower(ss[c]);} //convert the user's input to lower
- case
- for(c=0;c<num;c++)
- {if(!strcmp(ss,friend[c]->name[]));
- {printf("\n #%d\n",c+1);
- printf(" name:%s sex:%c\n age:%d\n",friend[c]->name[],friend[c]->sex,
- friend[c]->age);
- match=1;}
- }
- if(match==0)
- puts(" No match found.");
-
- return;
- }
- //********************************************************************
- void dealloc(struct list *friend[])
- {
- int c;
- for(c=0;c<num;c++)
- {
- free(friend[c]);
- }
- return;
- }
-
-
- That's all . THANX!
-
-
-
-
-
-
-
-
-